package com.hut.oa.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.support.SessionStatus;

import com.hut.oa.model.bean.User;
import com.hut.oa.model.bean.WorkReport;
import com.hut.oa.model.bean.WorkReportComment;
import com.hut.oa.model.dao.user.DeptDao;
import com.hut.oa.model.dao.user.UserDao;
import com.hut.oa.model.dao.workreport.WorkReportCommentDao;
import com.hut.oa.model.dao.workreport.WorkReportDao;
import com.mysql.fabric.xmlrpc.base.Array;

import javassist.expr.NewArray;

@Controller
@RequestMapping("/work/report")
public class WorkReportController {
	
	@Autowired
	DeptDao dd;
	
	@Autowired
	WorkReportDao wrd;

	@Autowired
	UserDao ud;
	
	@Autowired
	WorkReportCommentDao wrcd;

	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

	/**
	 * 我的报告
	 * 
	 * @return
	 */
	@RequestMapping
	public String presentation(Model md, HttpSession session) {
		User user = (User) session.getAttribute("userinfo");
		Calendar calendar = Calendar.getInstance();
		calendar.setFirstDayOfWeek(Calendar.MONDAY);
		Integer time = calendar.get(Calendar.WEEK_OF_YEAR) - 1;
		md.addAttribute("wr", wrd.findRead(user.getId(), "week", time, 2017));
		return "work_report/presentation";
	}

	/**
	 * 查阅报告
	 * 
	 * @return
	 * @throws ParseException
	 */
	@RequestMapping("/consult")
	public String consult(String key,Long did,WorkReport wr, Model model, @PageableDefault(value = 10, sort = { "id" }) Pageable pageable)
			throws ParseException {
		int year = 0;
		Calendar calendar = Calendar.getInstance();
		if (wr.getYear() == null) {
			year = calendar.get(Calendar.YEAR);
		} else {
			year = wr.getYear();
		}
		if (wr.getType() == null) {
			model.addAttribute("type", "week");
		} else {
			model.addAttribute("type", wr.getType());
		}
		model.addAttribute("year", (year + "").replace(",", ""));
		Page<User> user = ud.findAll(pageable);
		if (key!=null||did!=null) {
			if (did!=null) {
				user = ud.findByRealNameAndDept(key, dd.findOne(did), pageable);
			}else{
				user = ud.findByRealNameLikeLike(key, pageable);
			}
			
		}
		model.addAttribute("userpage", user);
		List<User> users = user.getContent();
		if (wr.getType() == null || wr.getType().equals("week")) {
			List<String> head = alw(year);
			model.addAttribute("users", allweek(users, head.size()));
			model.addAttribute("head", head);
		} else if (wr.getType().equals("month")) {
			List<String> head = alm(year);
			model.addAttribute("users", allmonth(users, head.size()));
			model.addAttribute("head", head);
		} else if (wr.getType().equals("season")) {
			List<String> head = alse();
			model.addAttribute("users", allseason(users));
			model.addAttribute("head", head);
		} else {
			List<String> head = aly();
			model.addAttribute("users", allyear(users));
			model.addAttribute("head", head);
		}
		model.addAttribute("depts", dd.findAllByOrderByGrade());
		return "work_report/workreport_consult";
	}

	/**
	 * 报告统计
	 * @return
	 * @throws ParseException 
	 */
	/*@RequestMapping("/statistics")
	public String statistics(String type,Model model) throws ParseException {
		if (type==null||type=="") {
			type="week";
		}
		Map<String, Object> s = new HashMap<>();
		if (type=="week") {
			s.put("y",alw(2017));
		}else if (type.equals("month")) {
			s.put("y",alm(2017));
		}else{
			s.put("y",aly());
		}
		List<Map<String, Object>> sw=new ArrayList<>();
		sw.add(s);
		model.addAttribute("type", type);
		model.addAttribute("wwe", sw);
		return "work_report/workreport_statistics";
	}*/
	
	//报告统计查询
	@RequestMapping("/statistics")
	public String statistics(String key,String type, Model model, @PageableDefault(value = 10, sort = { "id" }) Pageable pageable) throws ParseException {
		if (type==null||type=="") {
			type="week";
		}
		Page<User> user = ud.findAll(pageable);
		if (key!=null) {
			user = ud.findByRealNameLikeLike(key, pageable);
		}
		model.addAttribute("user", user);
		List<User> users = user.getContent();
		List<Map<String,Object>> statistic = new ArrayList<>();
		for (User user2 : users) {
			Map<String, Object> s=new HashMap<String, Object>();
			s.put("name", user2.getRealName());
			int ti=wrd.findReads(user2.getId(), type, 2017, 1).size();
			s.put("ti", ti);
			if (type=="week") {
				s.put("wei",alw(2017).size()-ti);
				s.put("zong", alw(2017).size());
			}else if (type.equals("month")) {
				s.put("wei",alm(2017).size()-ti);
				s.put("zong", alm(2017).size());
			}else{
				s.put("wei",2-ti);
				s.put("zong",2);
			}
			
			statistic.add(s);
		}
		model.addAttribute("type", type);
		model.addAttribute("statistic", statistic);
		return "work_report/workreport_statistics";
	}
	/**
	 * 查阅报告
	 * 
	 * @param type
	 * @param time
	 * @param session
	 * @param md
	 * @return
	 */
	@RequestMapping("/read")
	public String read(Long id, Model md) {
		md.addAttribute("wr", wrd.findOne(id));
		md.addAttribute("user", ud.findOne(wrd.findOne(id).getUserId()));
		List<WorkReportComment> evaluate = wrcd.findByWsId(id);
		for (WorkReportComment workReportComment : evaluate) {
			workReportComment.setUser(ud.findOne(workReportComment.getUserId()));
		}
		md.addAttribute("evaluate", evaluate);
		return "work_report/read_workreport";
	}

	/**
	 * 查询评价
	 */
	@ResponseBody
	@RequestMapping("/select/evaluate")
	public List<WorkReportComment> sewrc(Long id) {
		List<WorkReportComment> evaluate = wrcd.findByWsId(id);
		for (WorkReportComment workReportComment : evaluate) {
			workReportComment.setUser(ud.findOne(workReportComment.getUserId()));
		}
		return evaluate;
	}

	/**
	 * 保存
	 * 
	 * @param wr
	 * @param session
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/seav")
	public WorkReport seav(WorkReport wr, HttpSession session) {
		User user = (User) session.getAttribute("userinfo");
		wr.setUserId(user.getId());
		if (wr.getRelease() == null) {
			wr.setRelease(0);
		}
		wrd.save(wr);
		if (wrd.save(wr) != null) {
			return wr;
		} else {
			return null;
		}
	}

	/**
	 * 通过类型，时间查找某一篇报告
	 * 
	 * @param type
	 * @param time
	 * @param session
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/select")
	public WorkReport select(String type, Integer time, HttpSession session) {
		User user = (User) session.getAttribute("userinfo");
		return wrd.findRead(user.getId(), type, time, 2017);
	}

	/**
	 * 发表评价
	 */
	@ResponseBody
	@RequestMapping("/save/evaluate")
	public WorkReportComment select(WorkReportComment wrc, HttpSession session) {
		User user = (User) session.getAttribute("userinfo");
		wrc.setUserId(user.getId());
		if (wrcd.save(wrc) != null) {
			WorkReportComment evaluate=wrcd.save(wrc);
			evaluate.setUser(ud.findOne(evaluate.getUserId()));
			return evaluate;
		} else {
			return null;
		}

	}

	/**
	 * 查询所有周报告
	 */
	private List<User> allweek(List<User> users, Integer week) {
		for (User user : users) {
			List<WorkReport> wr = new ArrayList<>();
			List<WorkReport> wrs = wrd.findReads(user.getId(), "week", 2017, 1);
			for (int i = week; i > 0; i--) {
				Integer ss = 0;
				for (WorkReport workReport : wrs) {
					if (workReport.getTime() == i) {
						wr.add(workReport);
						ss = 1;
					}
				}
				if (ss == 0) {
					wr.add(null);
				}
			}
			user.setWorkReport(wr);
		}
		return users;
	}

	/**
	 * 返回指定年所有周集合（已过的）
	 * 
	 * @throws ParseException
	 */
	private List<String> alw(Integer year) throws ParseException {
		Date now = new Date();
		Calendar calendar = Calendar.getInstance();
		if (year != calendar.get(Calendar.YEAR)) {
			now = format.parse(year + "-12-31");
		}
		calendar.setFirstDayOfWeek(Calendar.MONDAY);
		calendar.setTime(now);
		Integer week = calendar.get(Calendar.WEEK_OF_YEAR) - 1;
		List<String> head = new ArrayList<>();
		for (int i = week; i > 0; i--) {
			head.add("第" + i + "周");
		}
		return head;

	}

	/**
	 * 查询所有月报告
	 */
	private List<User> allmonth(List<User> users, int month) {
		for (User user : users) {
			List<WorkReport> wr = new ArrayList<>();
			List<WorkReport> wrs = wrd.findReads(user.getId(), "month", 2017, 1);
			for (int i = month; i > 0; i--) {
				Integer ss = 0;
				for (WorkReport workReport : wrs) {
					if (workReport.getTime() == i) {
						wr.add(workReport);
						ss = 1;
					}
				}
				if (ss == 0) {
					wr.add(null);
				}
			}
			user.setWorkReport(wr);
		}
		return users;
	}

	/**
	 * 返回指定年所有月集合（已过的）
	 */
	private List<String> alm(Integer year) throws ParseException {
		Calendar now = Calendar.getInstance();
		Integer month = 12;
		if (year.equals(now.get(Calendar.YEAR))) {
			month = now.get(Calendar.MONTH) + 1;
		}
		List<String> head = new ArrayList<>();
		for (int i = month; i > 0; i--) {
			head.add(i + "月份");
		}
		return head;
	}

	/**
	 * 查询所有季报告
	 */
	private List<User> allseason(List<User> users) {
		for (User user : users) {
			List<WorkReport> wr = new ArrayList<>();
			List<WorkReport> wrs = wrd.findReads(user.getId(), "season", 2017, 1);
			for (int i = 1; i < 5; i += 2) {
				Integer ss = 0;
				for (WorkReport workReport : wrs) {
					if (workReport.getTime() == i) {
						wr.add(workReport);
						ss = 1;
					}
				}
				if (ss == 0) {
					wr.add(null);
				}
			}
			user.setWorkReport(wr);
		}
		return users;
	}

	/**
	 * 返回指定年所有季度集合
	 */
	private List<String> alse() throws ParseException {
		List<String> head = new ArrayList<>();
		head.add("第一季度");
		head.add("第三季度");
		return head;
	}

	/**
	 * 查询所有年报告
	 */
	private List<User> allyear(List<User> users) {
		for (User user : users) {
			List<WorkReport> wr = new ArrayList<>();
			List<WorkReport> wrs = wrd.findReads(user.getId(), "year", 2017, 1);
			for (int i = 1; i < 5; i += 2) {
				Integer ss = 0;
				for (WorkReport workReport : wrs) {
					if (workReport.getTime() == i) {
						wr.add(workReport);
						ss = 1;
					}
				}
				if (ss == 0) {
					wr.add(null);
				}
			}
			user.setWorkReport(wr);
		}
		return users;
	}

	/**
	 * 返回指定年所有年度集合
	 */
	private List<String> aly() {
		List<String> head = new ArrayList<>();
		head.add("年中");
		head.add("年终");
		return head;
	}
}
